package log

import (
	"github.com/cloudwego/hertz/pkg/common/hlog"
	hertzzap "github.com/hertz-contrib/logger/zap"
	"go.uber.org/zap"
	"gopkg.in/natefinch/lumberjack.v2"
	"log"
	"os"
	"path"
	"time"
)

func InitLog(mode string) {

	if mode != "dev" {
		var logFilePath string
		dir := "./hlog"
		logFilePath = dir + "/logs/"
		if err := os.MkdirAll(logFilePath, 0o777); err != nil {
			log.Println(err.Error())
			return
		}

		logFileName := time.Now().Format("2006-01-02") + ".log"
		fileName := path.Join(logFilePath, logFileName)
		if _, err := os.Stat(fileName); err != nil {
			if _, err := os.Create(fileName); err != nil {
				log.Println(err.Error())
				return
			}
		}
		logger := hertzzap.NewLogger(hertzzap.WithZapOptions(zap.AddCaller(), zap.AddCallerSkip(3)))

		lumberjackLogger := &lumberjack.Logger{
			Filename:   fileName,
			MaxSize:    20,   // A file can be up to 20M.
			MaxBackups: 5,    // Save up to 5 files at the same time.
			MaxAge:     10,   // A file can exist for a maximum of 10 days.
			Compress:   true, // Compress with gzip.
		}
		logger.SetOutput(lumberjackLogger)
		logger.SetLevel(hlog.LevelDebug)

		hlog.SetLogger(logger)
		hlog.Info("Hello, hertz")

	}

	return

}
